// pages/cash_withdrawal/cash_withdrawal.js
const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS


import { Cash_Withdrawal } from 'cash_withdrawal.model.js';
var withdrawal = new Cash_Withdrawal(); //调用request请求的对象

var all_withdrawals = '';
var reward = 0;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showIcon: true //显示返回键以及首页

  },
  //确认提现表单提交，发送网络提现请求，提现到微信零钱
  formSubmit: function (e) {
    var amount_of_money = e.detail.value.amount_of_money;

    if (amount_of_money > reward) {
      tt.showToast({
        title: '提现金额大于拥有赏金',
        icon: 'none',
        duration: 3000
      });
    } else {
      if (amount_of_money < 1) {
        tt.showToast({
          title: '提现金额不足',
          icon: 'none',
          duration: 3000
        });
      } else {
        //也能用PHP那种直接判断的方法
        if (amount_of_money) {
          console.log(123);
          amount_of_money = amount_of_money * 1;
          console.log(amount_of_money);
          withdrawal.getCash_withdrawal(amount_of_money, row => {
            console.log(row);

            if (row == '提现成功') {
              tt.showToast({
                title: '提现成功',
                icon: 'none',
                duration: 3000
              });
              this.onLoad();
            } else {
              tt.showToast({
                title: '提现失败',
                icon: 'none',
                duration: 3000
              });
            }
          });
        }

        console.log(amount_of_money);
      }
    }
  },
  all_withdrawals: function (e) {
    console.log(reward);
    this.setData({
      amount_of_money: reward
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    withdrawal.getbalance(row => {
      console.log(row);

      if (row == 'null') {
        this.setData({
          'reward': 0
        });
      } else {
        reward = row;
        this.setData({
          'reward': row
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});